package com.atguigu.yygh.msm.controller;

import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.msm.service.MsmService;
import com.atguigu.yygh.msm.utils.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/api/msm")
public class MsmApiController {

    @Autowired
    private MsmService msmService;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    //发送手机验证码
    @GetMapping("send/{phone}")
    public Result sendCode(@PathVariable String phone){
        //先从redis里获取验证码，如果获取到返回ok(判断redis中是否已有此手机号的验证码)
        //key-手机号，value-验证码
        /*String code = redisTemplate.opsForValue().get(phone);
        if(!StringUtils.isEmpty(code)){
            return Result.ok();
        }*/

        //获取不到则生成验证码
        String code = RandomUtil.getSixBitRandom();
        //调用service实现，通过整合短信服务进行发送
        //boolean isSend = msmService.send(phone,code);
        //将生成的验证码放入redis，设置有效时间
        //if(isSend){
            redisTemplate.opsForValue().set(phone,code,2, TimeUnit.MINUTES);  //key,value,时间，时间单位
            return Result.ok();
        /*}else{  //发送验证码失败
            return Result.fail().message("发送短信失败");
        }*/
    }
}
